package com.handpay.nocardpay.baselibrary;

import android.annotation.TargetApi;
import android.app.Application;
import android.os.Build;
import android.os.StrictMode;

import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.location.LocationService;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.logutils.PlainLogger;
import com.handpay.nocardpay.baselibrary.logutils.Storage;
import com.handpay.nocardpay.baselibrary.logutils.StorageUtility;
import com.handpay.nocardpay.baselibrary.threadmanager.ThreadPoolFactory;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.umeng.analytics.MobclickAgent;

public class NoCardPayApplication extends Application {

    private static final String TAG = "NoCardPayApplication";
    public static NoCardPayApplication noCardPayApplication = null;
    public LocationService locationService;


    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onCreate() {
        super.onCreate();
        noCardPayApplication = this;
        ThreadPoolFactory.getNormalPool().execute(new Runnable() {
            @Override
            public void run() {
                /**
                 *初始化日志工具类
                 */
                String logFilePath = StorageUtility.combinePath(getApplicationInfo().dataDir, "plainlog.txt");
                HPLog.SetLogger(PlainLogger.instance(NoCardPayConfig.PRINT_LOG, logFilePath));
                Storage.initialize(getApplicationContext(), BaseProtocolKey.Dir_NAME);
                /**
                 * 初始化友盟同级
                 */
                //启动友盟统计相关
                MobclickAgent.UMAnalyticsConfig umAnalyticsConfig = new MobclickAgent.UMAnalyticsConfig(noCardPayApplication, NoCardPayConfig.UMENG_APPKEY, NoCardPayConfig.CLIENTCHANNEL, MobclickAgent.EScenarioType.E_UM_NORMAL, false);
                MobclickAgent.startWithConfigure(umAnalyticsConfig);
                MobclickAgent.setDebugMode(true);
                //7.0以后
                StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
                StrictMode.setVmPolicy(builder.build());
                //初始化定位
                locationService = new LocationService(getApplicationContext());
                locationService.registerListener();
                locationService.start();
            }
        });
    }

    public static NoCardPayApplication getApp() {
        return noCardPayApplication;
    }

    public AccountUtils.Account mAccount;

    public void setAccount(AccountUtils.Account account) {
        this.mAccount = account;
    }

    public AccountUtils.Account getAccount() {
        return mAccount;
    }

    public void exitAppaction() {
        locationService.unregisterListener();
        locationService.stop();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
